//
//  ST_ReceivedGoodsListViewController.swift
//  btvideo
//
//  Created by mac on 2020/7/2.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

/**
 暂时弃用
 */
class ST_ReceivedGoodsListViewController: ST_BTLightBarViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title_nav_btn.setTitleForAllStates("我的伴心密友")
        
        
        self.view.addSubview(rank_btn)
        self.view.addSubview(receive_gift_btn)
        
        rank_btn.snp.makeConstraints { (make) in
            make.width.equalTo(86)
            make.height.equalTo(34)
            make.top.equalToSuperview().offset(statusBarHeight+44+10)
            make.right.equalTo(self.view.snp.centerX).offset(-10)
        }
        receive_gift_btn.snp.makeConstraints { (make) in
            make.top.width.height.equalTo(rank_btn)
            make.left.equalTo(self.view.snp.centerX).offset(10)
        }
        
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.top.equalTo(rank_btn.snp.bottom).offset(10)
        }
        
        
        scrollView.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalTo(ScreenWidth*2)
            make.height.equalToSuperview()
        }
        
        
        
        self.addChild(rank_controller)
        self.addChild(history_controller)
        
        root_view.addSubview(rank_controller.view)
        root_view.addSubview(history_controller.view)
        
        rank_controller.view.snp.makeConstraints { (make) in
            make.width.equalTo(ScreenWidth)
            make.top.bottom.left.equalToSuperview()
        }
        
        history_controller.view.snp.makeConstraints { (make) in
            make.width.equalTo(ScreenWidth)
            make.top.bottom.right.equalToSuperview()
        }
        
        
        
        
    }
    
    
//    MARK: - Properties
    fileprivate lazy var rank_controller = ST_GiftRankViewController()
    
    fileprivate lazy var history_controller:ST_GiftHistoryViewController = {
       let control = ST_GiftHistoryViewController()
        return control
    }()
    
    fileprivate lazy var scrollView:UIScrollView = {
       let control = UIScrollView()
        control.backgroundColor = UIColor.white
        control.isPagingEnabled = true
        control.delegate = self
       return control
    }()
    
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.white
        return control
    }()
    
     fileprivate lazy var last_btn = rank_btn
     
    fileprivate lazy var btn_selected_image:UIImage = {
        let img = UIImage.init(color: UIColor.init(hexString: "#FEC207")!, size: CGSize.init(width: 1, height: 1))
        return img
    }()
    
    fileprivate lazy var btn_normal_image:UIImage = {
        let img = UIImage.init(color: UIColor.white, size: CGSize.init(width: 1, height: 1))
        return img
    }()
    
     fileprivate lazy var rank_btn:UIButton = {
         
         let control = UIButton.init(type: UIButton.ButtonType.custom)
         control.setBackgroundImage(btn_selected_image, for: UIControl.State.selected)
         control.setBackgroundImage(btn_normal_image, for: UIControl.State.normal)
         control.isSelected = true
         control.cornerRadius = 17
         control.setTitleForAllStates("密友排名")
         control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
         control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
         control.setTitleColor(UIColor.white, for: UIControl.State.selected)
         control.addTarget(self, action: #selector(click_switch_controller_handle(sender:)), for: UIControl.Event.touchUpInside)
         return control
     }()
     
    
     
     
     
     
     fileprivate lazy var receive_gift_btn:UIButton = {
             
         let control = UIButton.init(type: UIButton.ButtonType.custom)
         control.setBackgroundImage(btn_selected_image, for: UIControl.State.selected)
         control.setBackgroundImage(btn_normal_image, for: UIControl.State.normal)
         control.cornerRadius = 17
         control.setTitleForAllStates("收礼记录")
         control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
         control.setTitleColor(UIColor.white, for: UIControl.State.selected)
         control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
         control.addTarget(self, action: #selector(click_switch_controller_handle(sender:)), for: UIControl.Event.touchUpInside)
         return control
     }()
}



extension ST_ReceivedGoodsListViewController{
    
    @objc func click_switch_controller_handle(sender:UIButton){
        
        if sender.isSelected{
            return
        }
        
        last_btn.isSelected = false
        
        sender.isSelected = true
        
        last_btn = sender
        
        scrollView.setContentOffset(CGPoint.init(x: sender == receive_gift_btn ? ScreenWidth : 0, y: 0), animated: true)
        
    }
    
    
}


extension ST_ReceivedGoodsListViewController:UIScrollViewDelegate{
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let  page = (scrollView.contentOffset.x + (0.5 * ScreenWidth)) / ScreenWidth
        
        if  page.int == 0{
            click_switch_controller_handle(sender: self.rank_btn)
        }else{
            click_switch_controller_handle(sender: self.receive_gift_btn)
        }
    }
    
}


